﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bus.Bll;
using Bus.Model;


public partial class admin_adminCus : System.Web.UI.Page
{
    private tb_custom cusBll = new tb_custom();
    protected tb_cost costbll = new tb_cost();
    protected CostModel cm = null;
    private LogModel lm = null;
    protected CustomModel cusM = null;
    protected tb_log logbll = new tb_log();
    protected string strWhere = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ProviderPageData();
        }

    }
    private void ProviderPageData()
    {
        AspNetPager1.PageSize = 20;
        AspNetPager1.RecordCount = cusBll.GetRecordCount(strWhere);
        cusrep.DataSource = cusBll.GetListByPage(strWhere, "cid desc", (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize);
        cusrep.DataBind();
    }
    public string ToSex(object str)
    {
        if (str.ToString() == "0")
            return "男";
        else
            return "女";
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        ProviderPageData();
    }
    protected void LinkButton1_click(object sender, EventArgs e)
    {
        try
        {
            LinkButton lbt = sender as LinkButton;
            int cid = int.Parse(lbt.CommandArgument);
            string command = lbt.CommandName;
            if (command == "lost")
            {
                try
                {
                    cusM = new CustomModel();
                    cusM = cusBll.GetModel(cid);
                    if (cusM.islost)
                    {
                        if (cusBll.Lost(cid, false) > 0)
                        {
                            //取消乘客挂失状态
                            Bus.Dal.DbHelperSQL.ExecuteSql("update tb_custom set nm='' where cid=" + cid);
                        }
                    }
                    else
                    {
                        cusBll.Lost(cid, true);//乘客挂失状态
                    }
                    lm = new LogModel();
                    lm.userid = int.Parse(Session["userid"].ToString());
                    lm.op = "挂失";
                    lm.memo = "挂失信息";
                    lm.opdate = DateTime.Now;
                    logbll.Add(lm);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('挂失成功！');</script>");
                    ProviderPageData();
                }
                catch (Exception ae)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('挂失失败！');</script>");
                }
            }
            else if (command == "bu")
            {
                try
                {
                    //cm = new CostModel();
                    //cm = cusBll.GetModel(cid);
                    
                    cusBll.Lost(cid, false);//乘客取消挂失状态,办理补卡业务,其中的转帐的操作将在表中处理补上用乘客状态标识，补卡时启用该状态，与乘客表中关系对应，
                    lm = new LogModel();
                    lm.userid = int.Parse(Session["userid"].ToString());
                    lm.op = "补卡";
                    lm.memo = "补卡信息";
                    lm.opdate = DateTime.Now;
                    logbll.Add(lm);
                    Random rd = new Random((int)DateTime.Now.Ticks);
                    string nm = (DateTime.Now.Ticks + ((long)rd.Next(1000, 10000))).ToString();
                    int rows = Bus.Dal.DbHelperSQL.ExecuteSql("update tb_custom set nm='" + nm + "' where cid=" + cid);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('补卡成功！');alert('您的卡号【" + nm + "】');</script>");
                   // Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您的卡号【" + nm + "】');</script>");
                    ProviderPageData();
                }
                catch (Exception eee) { }
            }
            else if (command == "cz")
            {
                //乘客充值操作
                Response.Redirect("cz.aspx?id=" + cid);
            }
            else
            {
                if (cusBll.Delete(cid))//乘客挂失注销
                {
                    costbll.Delete(cid);
                    lm = new LogModel();
                    lm.userid = int.Parse(Session["userid"].ToString());
                    lm.op = "注销";
                    lm.memo = "注销一个乘客";
                    lm.opdate = DateTime.Now;
                    logbll.Add(lm);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('注销成功！');</script>");
                    ProviderPageData();
                }
                else
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('注销失败！');</script>");
            }
        }
        catch (Exception eee) { }
    }
    public string GetStatus(string s)
    {
        if (s.ToLower() == "true")
            return "是";
        else
            return "否";
    }
    public string GetCommondText(string s)
    {
        if (s.ToLower() == "true")
            return "取消挂失";
        else
            return "挂失";
    }

    protected void search_Click(object sender, EventArgs e)
    {
        if (searchtxt.Text == string.Empty)
            strWhere = "";
        else
            strWhere = " idnum='"+searchtxt.Text.Trim()+"' or nm='"+searchtxt.Text.Trim()+"'";
        ProviderPageData();
    }
}
